package com.ljc.cjgl.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Marking;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.entitiy.Tea;
import com.ljc.cjgl.entitiy.TestRegister;
import com.ljc.cjgl.form.TestRegisterForm;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.ClassService;
import com.ljc.cjgl.service.MarkingService;
import com.ljc.cjgl.service.TestRegisterService;

@Controller
@RequestMapping(value="/grading")
public class GradingContorller {
	
	@Autowired
	private TestRegisterService service;
	@Autowired
	private ClassService classService;
	@Autowired
	private MarkingService markingService;
	@Autowired
	private UserUtils userUtils;
	@RequestMapping("/manage")
	@PreAuthorize("hasRole('ROLE_TEACHER')")
	public String manage(ModelMap map) {
		Tea teacher=null;
		List<TbTest> tests=new ArrayList<>();
		try {
			teacher=(Tea)userUtils.getUser();
			Integer teacherId=teacher.getId();
			List<Marking> markings = markingService.findByTeacherId(teacherId);
			for (Marking marking : markings) {
				tests.add(marking.getTest());
			}
			List<TbClass> classes=classService.findAll();
			map.put("tests", tests);
			map.put("classes",classes);
			return "grading/manage";
		} catch (Exception e) {
			return "redirect:/logout";
		}
	}
	
	private Specification<TestRegister> bulidSpec(TestRegisterForm form){
		HashSet<Rule> rules=new HashSet<>();
		List<Integer> testIds = form.getTestIds();
		if(testIds.size()>0) {
			rules.add(Rule.property("test.id").in(form.getTestIds()));
		}else {
			rules.add(Rule.property("test.id").isNull());
		}
		Integer testId = form.getTestId();
		Integer classId = form.getClassId();
		String search=form.getSearch();
		if(testId!=null) {
			rules.add(Rule.property("test.id").equal(testId));
		}
		if(classId!=null) {
			rules.add(Rule.property("student.tbClass.id").equal(classId));
		}
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("test.name").contain(search));
			rules2.add(Rule.property("student.name").contain(search));
			rules2.add(Rule.property("student.tbClass.name").contain(search));
			rules2.add(Rule.property("projectUrl").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(TablePageable pageable,TestRegisterForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Tea teacher=(Tea)userUtils.getUser();
		List<Marking> markings=markingService.findByTeacherId(teacher.getId());
		List<Integer> ids=new ArrayList<>();
		for (Marking marking : markings) {
			ids.add(marking.getTest().getId());
		}
		form.setTestIds(ids);
		Specification<TestRegister> spec=bulidSpec(form);
		Page<TestRegister> page=service.findAll(spec, pageRequest);
		return TableResult.bulidPageResult(page);
	}

}
